package cn.lovemmd.bms.service.Impl;

import cn.lovemmd.bms.mapper.AdminMapper;
import cn.lovemmd.bms.pojo.Admin;
import cn.lovemmd.bms.pojo.Code;
import cn.lovemmd.bms.service.AdminService;
import cn.lovemmd.bms.service.CodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceImpl implements AdminService {
    private final AdminMapper adminMapper;
    private final CodeService codeService;

    @Autowired
    public AdminServiceImpl(AdminMapper adminMapper, CodeService codeService) {
        this.adminMapper = adminMapper;
        this.codeService = codeService;
    }

    @Override
    public Integer signOut(int userId) {
        return adminMapper.signOut(userId);
    }

    @Override
    public Integer updatePassword(int userId, String userPassword) {
        return adminMapper.updatePassword(userId, userPassword);
    }

    @Override
    public Admin login(String userName, String userPassword) {
        return adminMapper.login(userName, userPassword);
    }

    @Override
    public Integer register(String userName, String userPassword, String userAuthority) {
        Code code = codeService.getCode(userAuthority);
        if (code == null) {
            return -2;
        } else if (code.getIsUse() == 1) {
            return -1;
        } else {
            codeService.useCode(userAuthority);
            return adminMapper.register(userName, userPassword, code.getAuthority());
        }
    }
}
